package com.huifaapp.mall.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huifaapp.mall.R;
import com.huifaapp.mall.uitils.Contig;
import com.huifaapp.mall.uitils.DownImage;
import com.huifaapp.mall.uitils.ImgUtils;
import com.huifaapp.mall.uitils.UtilsImage;
import com.huifaapp.mall.view.InviteCollarWin;
import com.huifaapp.mall.view.PosterWin;
import com.huifaapp.mall.view.TakePhotoPopWin;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Administrator on 2018/1/16.
 */

public class PosterActivity extends AppCompatActivity implements View.OnClickListener{

    private static File foder;
    PosterWin mPop;

    private TextView tv_weixinhaoyou,tv_pengyouquan,tv_qqhaoyou,tv_xinlangweibo,tv_baocunshouji,tv_yqrz_poster_cancel;
    ImageView iv_poster_activity;
    String invitingimg;
    private LinearLayout poster_po;
    private ImageView iv_ruzhubg;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yqrz_poster_activity);
//        iv_poster_activity= (ImageView) findViewById(R.id.iv_poster_activity);
        tv_weixinhaoyou= (TextView)findViewById(R.id.tv_weixinhaoyou_poster);
        tv_pengyouquan= (TextView)findViewById(R.id.tv_pengyouquan_poster);
        tv_weixinhaoyou.setOnClickListener(this);
        tv_pengyouquan.setOnClickListener(this);
        tv_qqhaoyou= (TextView) findViewById(R.id.tv_qqhaoyou_poster);
        tv_qqhaoyou.setOnClickListener(this);
        tv_xinlangweibo= (TextView)findViewById(R.id.tv_xinlangweibo_poster);
        tv_xinlangweibo.setOnClickListener(this);
        tv_baocunshouji= (TextView)findViewById(R.id.tv_baocunshouji_poster);
        tv_baocunshouji.setOnClickListener(this);
        tv_yqrz_poster_cancel= (TextView) findViewById(R.id.tv_yqrz_poster_cancel);
        poster_po= (LinearLayout) findViewById(R.id.poster_po);
        iv_ruzhubg= (ImageView) findViewById(R.id.iv_ruzhubg);
        tv_yqrz_poster_cancel.setOnClickListener(this);
        String qrcode=getIntent().getStringExtra("qrcode");
        invitingimg=getIntent().getStringExtra("invitingimg");
//        UtilsImage.uploadimageview(iv_poster_activity,qrcode,"",PosterActivity.this);
        UtilsImage.uploadimageview(iv_ruzhubg,invitingimg,"",PosterActivity.this);
//        poster_po.setBackgroundResource(invitingimg);


    }


    /** 首先默认个文件保存路径 */
    private static final String SAVE_PIC_PATH= Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory().getAbsolutePath() :"/mnt/sdcard";    //保存到SD卡
    private static final String SAVE_REAL_PATH = SAVE_PIC_PATH+" /good/savePic";//保存的确切位置

    public  void saveFile(Bitmap bm, String fileName) throws IOException {
        foder = new File(SAVE_REAL_PATH);
        if (!foder.exists()) {
            foder.mkdirs();
        }
        File myCaptureFile = new File(SAVE_REAL_PATH, fileName);
        if (!myCaptureFile.exists()) {
            myCaptureFile.createNewFile();
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
    }
    /*
     *    get image from network
     *    @param [String]imageURL
     *    @return [BitMap]image
     */
    public Bitmap returnBitMap(String url){
        URL myFileUrl = null;
        Bitmap bitmap = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_yqrz_poster_cancel:
                finish();
                break;
            case R.id.tv_baocunshouji_poster:
                try {
                    new DownImage().execute(invitingimg);
                    Log.e("SHUJU", Contig.bitmap+"            ");
                    if(Contig.bitmap.equals(null)||Contig.bitmap.equals("null")||Contig.bitmap.equals("")) {
                        new DownImage().execute(invitingimg);
                        Toast.makeText(PosterActivity.this,"您未点击到保存手机，请重新点击保存到手机!",Toast.LENGTH_SHORT).show();
                        ImgUtils.saveImageToGallery(PosterActivity.this, Contig.bitmap);
                    }else {
                        ImgUtils.saveImageToGallery(PosterActivity.this, Contig.bitmap);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case R.id.tv_weixinhaoyou_poster:
                UMImage image = new UMImage(PosterActivity.this,invitingimg);//网络图片
                shareUrl("1",null, null,image);
                break;
            case R.id.tv_pengyouquan_poster:
                UMImage image1 = new UMImage(PosterActivity.this,invitingimg);//网络图片
                shareUrl("2",null, null,image1);
                break;
            case R.id.tv_qqhaoyou_poster:
                UMImage imageqq = new UMImage(PosterActivity.this,invitingimg);//网络图片
                shareUrl("3",null, null,imageqq);
                break;
            case R.id.tv_xinlangweibo_poster:
                UMImage imagesl = new UMImage(PosterActivity.this,invitingimg);//网络图片
                shareUrl("4",null, null,imagesl);
                break;





        }
    }

    /**
     * 分享链接
     */

    private void shareUrl(String type, String con, UMWeb web, UMImage image) {

        android.util.Log.v("shiran","-----------shareUrl()----------");


        UMShareListener umShareListener = new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
//                if (dialog!=null&&!dialog.isShowing()) {
//                    dialog.show();
//                }
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享成功");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享成功");
                } else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享成功");
                } else if(share_media ==SHARE_MEDIA.SINA){
                    ToastUtils.showShortSafe("新浪微博分享成功");
                }else {
                    ToastUtils.showShortSafe("分享成功");

                }
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享失败");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享失败");
                }else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享失败");
                } else if(share_media ==SHARE_MEDIA.SINA) {
                    ToastUtils.showShortSafe("新浪微博分享失败");
                } else {
                    ToastUtils.showShortSafe("分享失败");

                }
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                if (share_media == SHARE_MEDIA.WEIXIN) {
                    ToastUtils.showShortSafe("微信分享取消");

                } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                    ToastUtils.showShortSafe("朋友圈分享取消");
                } else if(share_media ==SHARE_MEDIA.QQ){
                    ToastUtils.showShortSafe("QQ分享取消");
                } else if(share_media ==SHARE_MEDIA.SINA) {
                    ToastUtils.showShortSafe("新浪微博分享取消");
                } else {
                    ToastUtils.showShortSafe("分享取消");

                }
            }
        };

        if ("1".equals(type)) {  //分享微信
            new ShareAction((Activity) this)
                    .setPlatform(SHARE_MEDIA.WEIXIN).
                    setCallback(umShareListener)
                    .withText(con)
                    .withMedia(image)
                    .share();

        }else  if ("2".equals(type)) { //分享朋友圈

            new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).
                    setCallback(umShareListener)
                    .withMedia(image)
                    .withText(con)
                    .share();

        }else if("3".equals(type)){//分享qq
            new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.QQ).
                    setCallback(umShareListener)
                    .withMedia(image)
                    .withText(con)
                    .share();
        }else if("4".equals(type)){//分享微博
            new ShareAction((Activity) this).setPlatform(SHARE_MEDIA.SINA).
                    setCallback(umShareListener)
                    .withMedia(image)
                    .withText(con)
                    .share();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
